<!DOCTYPE HTML>
<html lang="zh-cn" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>CommonJS 规范 | Webpack 中文指南</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.5.2">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-ad/plugin.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./amd.html" />
    
    
    <link rel="prev" href="./advanced.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="3.1" data-chapter-title="CommonJS 规范" data-filepath="commonjs.md" data-basepath="." data-revision="Tue Feb 21 2017 16:37:20 GMT+0800 (CST)">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="preface.html">
            
                
                    <a href="./preface.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        前言
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="module-system.html">
            
                
                    <a href="./module-system.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        模块系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="what-is-webpack.html">
            
                
                    <a href="./what-is-webpack.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        重复的轮子
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="start.html">
            
                
                    <a href="./start.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        准备开始
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="install.html">
            
                
                    <a href="./install.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="usage.html">
            
                
                    <a href="./usage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="loader.html">
            
                
                    <a href="./loader.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Loader
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="configuration.html">
            
                
                    <a href="./configuration.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="plugin.html">
            
                
                    <a href="./plugin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        插件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="development.html">
            
                
                    <a href="./development.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="troubleshooting.html">
            
                
                    <a href="./troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        故障处理
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="advanced.html">
            
                
                    <a href="./advanced.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="commonjs.html">
            
                
                    <a href="./commonjs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        CommonJS 规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="amd.html">
            
                
                    <a href="./amd.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        AMD 规范
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="reference.html">
            
                
                    <a href="./reference.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        参考链接
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Webpack 中文指南</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="commonjs-&#x89C4;&#x8303;">CommonJS &#x89C4;&#x8303;</h1>
<p>CommonJS &#x662F;&#x4EE5;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x4E4B;&#x5916;&#x6784;&#x5EFA; JavaScript &#x751F;&#x6001;&#x7CFB;&#x7EDF;&#x4E3A;&#x76EE;&#x6807;&#x800C;&#x4EA7;&#x751F;&#x7684;&#x9879;&#x76EE;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x670D;&#x52A1;&#x5668;&#x548C;&#x684C;&#x9762;&#x73AF;&#x5883;&#x4E2D;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x9879;&#x76EE;&#x6700;&#x5F00;&#x59CB;&#x662F;&#x7531; Mozilla &#x7684;&#x5DE5;&#x7A0B;&#x5E08; Kevin Dangoor &#x5728;2009&#x5E74;1&#x6708;&#x521B;&#x5EFA;&#x7684;&#xFF0C;&#x5F53;&#x65F6;&#x7684;&#x540D;&#x5B57;&#x662F; ServerJS&#x3002;</p>
<blockquote>
<p>&#x6211;&#x5728;&#x8FD9;&#x91CC;&#x63CF;&#x8FF0;&#x7684;&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x6280;&#x672F;&#x95EE;&#x9898;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4EF6;&#x91CD;&#x5927;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x8BA9;&#x5927;&#x5BB6;&#x8D70;&#x5230;&#x4E00;&#x8D77;&#x6765;&#x505A;&#x51B3;&#x5B9A;&#xFF0C;&#x8FC8;&#x51FA;&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x6765;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x66F4;&#x5927;&#x66F4;&#x9177;&#x7684;&#x4E1C;&#x897F;&#x3002; &#x2014;&#x2014; Kevin Dangoor&apos;s <a href="http://www.blueskyonmars.com/2009/01/29/what-server-side-javascript-needs/" target="_blank">What Server Side JavaScript needs</a></p>
</blockquote>
<p>2009&#x5E74;8&#x6708;&#xFF0C;&#x8FD9;&#x4E2A;&#x9879;&#x76EE;&#x6539;&#x540D;&#x4E3A; CommonJS&#xFF0C;&#x4EE5;&#x663E;&#x793A;&#x5176; API &#x7684;&#x66F4;&#x5E7F;&#x6CDB;&#x5B9E;&#x7528;&#x6027;&#x3002;CommonJS &#x662F;&#x4E00;&#x5957;&#x89C4;&#x8303;&#xFF0C;&#x5B83;&#x7684;&#x521B;&#x5EFA;&#x548C;&#x6838;&#x51C6;&#x662F;&#x5F00;&#x653E;&#x7684;&#x3002;&#x8FD9;&#x4E2A;&#x89C4;&#x8303;&#x5DF2;&#x7ECF;&#x6709;&#x5F88;&#x591A;&#x7248;&#x672C;&#x548C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x3002;CommonJS &#x5E76;&#x4E0D;&#x662F;&#x5C5E;&#x4E8E; ECMAScript TC39 &#x5C0F;&#x7EC4;&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F46; TC39 &#x4E2D;&#x7684;&#x4E00;&#x4E9B;&#x6210;&#x5458;&#x53C2;&#x4E0E; CommonJS &#x7684;&#x5236;&#x5B9A;&#x3002;2013&#x5E74;5&#x6708;&#xFF0C;Node.js &#x7684;&#x5305;&#x7BA1;&#x7406;&#x5668; NPM &#x7684;&#x4F5C;&#x8005; Isaac Z. Schlueter &#x8BF4; <a href="https://github.com/nodejs/node-v0.x-archive/issues/5132#issuecomment-15432598" target="_blank">CommonJS &#x5DF2;&#x7ECF;&#x8FC7;&#x65F6;&#xFF0C;Node.js &#x7684;&#x5185;&#x6838;&#x5F00;&#x53D1;&#x8005;&#x5DF2;&#x7ECF;&#x5E9F;&#x5F03;&#x4E86;&#x8BE5;&#x89C4;&#x8303;</a>&#x3002;</p>
<p>CommonJS &#x89C4;&#x8303;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3; JavaScript &#x7684;&#x4F5C;&#x7528;&#x57DF;&#x95EE;&#x9898;&#x800C;&#x5B9A;&#x4E49;&#x7684;&#x6A21;&#x5757;&#x5F62;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x6BCF;&#x4E2A;&#x6A21;&#x5757;&#x5B83;&#x81EA;&#x8EAB;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;&#x6267;&#x884C;&#x3002;&#x8BE5;&#x89C4;&#x8303;&#x7684;&#x4E3B;&#x8981;&#x5185;&#x5BB9;&#x662F;&#xFF0C;&#x6A21;&#x5757;&#x5FC5;&#x987B;&#x901A;&#x8FC7; <code>module.exports</code> &#x5BFC;&#x51FA;&#x5BF9;&#x5916;&#x7684;&#x53D8;&#x91CF;&#x6216;&#x63A5;&#x53E3;&#xFF0C;&#x901A;&#x8FC7; <code>require()</code> &#x6765;&#x5BFC;&#x5165;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x7684;&#x8F93;&#x51FA;&#x5230;&#x5F53;&#x524D;&#x6A21;&#x5757;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x76F4;&#x89C2;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// moduleA.js</span>
<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> value </span>)</span>{
    <span class="hljs-keyword">return</span> value * <span class="hljs-number">2</span>;
}
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// moduleB.js</span>
<span class="hljs-keyword">var</span> multiplyBy2 = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./moduleA&apos;</span>);
<span class="hljs-keyword">var</span> result = multiplyBy2(<span class="hljs-number">4</span>);
</code></pre>
<p>CommonJS &#x662F;&#x540C;&#x6B65;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#xFF0C;&#x4F46;&#x5176;&#x5B9E;&#x4E5F;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x7AEF;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5176;&#x539F;&#x7406;&#x662F;&#x73B0;&#x5C06;&#x6240;&#x6709;&#x6A21;&#x5757;&#x90FD;&#x5B9A;&#x4E49;&#x597D;&#x5E76;&#x901A;&#x8FC7; <code>id</code> &#x7D22;&#x5F15;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x7684;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x73AF;&#x5883;&#x4E2D;&#x89E3;&#x6790;&#x4E86;&#xFF0C;&#x53EF;&#x4EE5;&#x53C2;&#x8003; <a href="https://github.com/Stuk/require1k" target="_blank">require1k</a> &#x548C; <a href="https://github.com/ruanyf/tiny-browser-require" target="_blank">tiny-browser-require</a> &#x7684;&#x6E90;&#x7801;&#x6765;&#x7406;&#x89E3;&#x5176;&#x89E3;&#x6790;&#xFF08;resolve&#xFF09;&#x7684;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x5173;&#x4E8E; CommonJS &#x89C4;&#x8303;&#x7684;&#x5185;&#x5BB9;&#x8BF7;&#x67E5;&#x770B; <a href="http://wiki.commonjs.org/wiki/CommonJS" target="_blank">http://wiki.commonjs.org/wiki/CommonJS</a>&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./advanced.html" class="navigation navigation-prev " aria-label="Previous page: 高级"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./amd.html" class="navigation navigation-next " aria-label="Next page: AMD 规范"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-github/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-editlink/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-baidu/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-ad/plugin.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"github":{"url":"https://github.com/zhaoda/webpack-handbook"},"editlink":{"base":"https://github.com/zhaoda/webpack-handbook/edit/master/content","label":"编辑本页","multilingual":false},"baidu":{"token":"a9787f0ab45d5e237bab522431d0a7ec"},"sharing":{"weibo":true,"twitter":false,"facebook":false,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"ad":{"contentBottom":"%3Cscript%20async%20src%3D%22//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js%22%3E%3C/script%3E%3C%21--%20%u81EA%u9002%u5E94%u9AD8%u53CD%u5DEE%u9ED1%u8272%20--%3E%3Cins%20class%3D%22adsbygoogle%22%20style%3D%22display%3Ablock%22%20data-ad-client%3D%22ca-pub-9798300025848807%22%20data-ad-slot%3D%228607007877%22%20data-ad-format%3D%22auto%22%3E%3C/ins%3E%3Cscript%3E%28adsbygoogle%20%3D%20window.adsbygoogle%20%7C%7C%20%5B%5D%29.push%28%7B%7D%29%3B%3C/script%3E%3C/p%3E%3Cdiv%20id%3D%22disqus_thread%22%3E%3C/div%3E%3Cscript%3E%28function%28%29%20%7Bvar%20d%20%3D%20document%2C%20s%20%3D%20d.createElement%28%27script%27%29%3Bs.src%20%3D%20%27//webpack-handbook.disqus.com/embed.js%27%3Bs.setAttribute%28%27data-timestamp%27%2C%20+new%20Date%28%29%29%3B%28d.head%20%7C%7C%20d.body%29.appendChild%28s%29%3B%7D%29%28%29%3B%3C/script%3E%3Cnoscript%3EPlease%20enable%20JavaScript%20to%20view%20the%20%3Ca%20href%3D%22https%3A//disqus.com/%3Fref_noscript%22%20rel%3D%22nofollow%22%3Ecomments%20powered%20by%20Disqus.%3C/a%3E%3C/noscript%3E"},"sitemap":{"hostname":"http://zhaoda.net/"},"prism":{},"highlight":{},"search":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <script>var _hmt = _hmt || [];(function() {var hm = document.createElement('script');hm.src = '//hm.baidu.com/hm.js?a9787f0ab45d5e237bab522431d0a7ec';var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(hm, s);})();</script>
    </body>
    
</html>
